package com.gitee.zhziyun.cnfsystem;

import com.gitee.zhziyun.cnfsystem.config.IpPortConfig;
import com.gitee.zhziyun.cnfsystem.master.AppMasterImpl;
import com.gitee.zhziyun.cnfsystem.utils.IpAddressUtil;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

public class AppCli {
    public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
        Scanner sc = new Scanner(System.in);
        System.out.print("输入用户名：");
        String userName = sc.nextLine();
        int avatarId = 0;

        String[] subnets = IpAddressUtil.getAllLocalSubnets();
        for (int i = 0; i < subnets.length; ++i) {
            System.out.println(i + " :" + subnets[i]);
        }
        int index;
        while (true) {
            System.out.print("输入序号选择子网：");
            try {
                index = sc.nextInt();
            } catch (Exception e) {
                sc.nextLine();
                continue;
            }
            if (index >= 0 && index < subnets.length) {
                break;
            }
        }
        IpPortConfig.setHeartBeatIp(IpAddressUtil.getBroadcastAddress(subnets[index]));

        int port;
        while (true) {
            System.out.printf("输入心跳端口(%d)：", IpPortConfig.getHeartBeatPort());
            try {
                port = sc.nextInt();
            } catch (Exception e) {
                sc.nextLine();
                continue;
            }
            if (port > 1023 && port < 65536) {
                break;
            }
        }
        IpPortConfig.setHeartBeatPort(port);

        new AppMasterImpl(userName, avatarId);
    }
}
